<?php
/*	Created by Alexander Alexandrov aka SS
			hazg@mail.ru				*/

define('SSLIB_UserOptions', TRUE);
if(!defined("SSLIB_Session")) die(__FILE__." Session class missing");
if(!defined("SSLIB_Database")) die(__FILE__." Database class missing");

class UserOptions
{
	static function Set($key, $val, $sql = true)
	{
		$userID = Session::UserID();
		global $gDB;
		if(($userID > 0) && $sql)
		{//���������������
			$data = $gDB->Query("SELECT * FROM user_options WHERE ownerid = '$userID' AND name = '$key'");
			if(!$data->IsEmpty())
				$gDB->Query("DELETE FROM user_options WHERE ownerid = '$userID' AND name = '$key'");
			$gDB->Query("INSERT INTO user_options (ownerid, name, val) VALUES ('$userID', '$key', '$val')");
		}
		setcookie($key, $val, time()+60*60*24*30, '/', strstr($_SERVER['HTTP_HOST'], '.'));	
	}
	static function Get($key, $default = null, $sql = true)
	{
		$userID = Session::UserID();
		global $gDB, $_COOKIE;
		$ret = null;
		if(($userID > 0) && $sql)
		{
			$data = $gDB->Query("SELECT val FROM user_options WHERE ownerid = '$userID' AND name = '$key'");
		
			if(!$data->IsEmpty())
			{
				$arr = $data->All();
				$ret = $arr['val'];
			}
			elseif(isset($_COOKIE[$key]))
			{
				$ret = $_COOKIE[$key];
			}
			else $ret = $default;
		}
		else
		{
			
			if(isset($_COOKIE[$key]))
			{
				$ret = $_COOKIE[$key];
			}
			else $ret = $default;
		}
		//echo $key;
		//print_r($_COOKIE);
		return $ret;
	}
}